package com.example.gokuplayalong.bases.mysql;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.example.gokuplayalong.bases.base.ExceptionHandler;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class MysqlDruid {
    public static DataSource dataSource = null;
    static Properties properties = null;
    static String propertiesFile = null;
    static {
        System.setProperty("druid.mysql.usePingMethod","false");
    }

    public static void Init() {
        properties = new Properties();
        try {
            InputStream inputStream = new ClassPathResource("druid-config.yaml").getInputStream();

            properties.load(inputStream);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }
    }

    public static void test() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("select * from profileinfo;");

            rs = pstmt.executeQuery();
            while (rs.next()) {
                //System.out.println(rs.getString("userId"));
            }
            conn.close();

        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }


    }

}
